package com.credithc.eagleeye.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
/**
 * 描 述: <br/>
 * 作 者: zhangjin<br/>
 * 创 建：2017年04月22日<br/>
 * 版 本：v1.0.0<br>
 * <p>
 * 历 史: (版本) 作者 时间 注释 <br/>
 */
@SuppressWarnings("ALL")
public class FastJsonConvert {

    private static final Logger LOGGER = LoggerFactory.getLogger(FastJsonConvert.class);

    private static final SerializerFeature[] featuresWithNullValue = { SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullBooleanAsFalse,
            SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullStringAsEmpty };
    private static final SerializerFeature[] featuresWithPrettyFormat = { SerializerFeature.PrettyFormat };

    /**
     * JsonString 转换 Object
     *
     * @param <T>
     * @param data
     * @param clzss
     * @return
     */
    public static <T> T convertJSONToObject(String data, Class<T> clzss) {
        try {
            T t = JSON.parseObject(data, clzss);
            return t;
        } catch (Exception e) {
            LOGGER.error("fastjson转换异常："+e.getMessage());
            return null;

        }
    }
    /**
     * JsonString 转换 List<Object>
     *
     * @param <T>
     * @param data
     * @param clzss
     * @return
     */
    public static <T> List<T> convertJSONToArray(String data, Class<T> clzss) {
        try {
            List<T> t = JSON.parseArray(data, clzss);
            return t;
        } catch (Exception e) {
            LOGGER.error("fastjson转换异常："+e.getMessage());
            return null;

        }
    }

    /**
     * Object to JsonString
     *
     * @param <T>
     * @param data
     * @param valueType
     * @return
     */
    public static String convertObjectToJSON(Object obj) {
        try {
            String text = JSON.toJSONString(obj);
            return text;
        } catch (Exception e) {
            LOGGER.error("fastjson转换异常："+e.getMessage());
            return null;
        }
    }
    /**
     * Object to JsonString
     *
     * @param <T>
     * @param data
     * @param valueType
     * @return
     */
    public static String convertObjectToJsonWithPrettyFormat(Object obj) {
        try {
            String text = JSON.toJSONString(obj,featuresWithPrettyFormat);
            return text;
        } catch (Exception e) {
            LOGGER.error("fastjson转换异常："+e.getMessage());
            return null;
        }
    }

    /**
     * Object to JsonString
     *
     * @param <T>
     * @param data
     * @param valueType
     * @return
     */
    public static String convertObjectToJSONWithNullValue(Object obj) {
        try {
            String text = JSON.toJSONString(obj, featuresWithNullValue);
            return text;
        } catch (Exception e) {
            LOGGER.error("fastjson转换异常："+e.getMessage());
            return null;
        }
    }
}
